<?php
/**
 * @version $Id: mod_extensionicons2.php 273 2007-12-06 18:27:26Z nik.elkuku $
 * repo: $URL: http://joomla-codebase.googlecode.com/svn/trunk/extensionicons2/administrator/modules/mod_extensionicons2/mod_extensionicons2.php $
 * Created on 14.04.2007
 * @package ExtensionIcons2
 * @author Nikolai Plath (elkuku)
 */

// no direct access
defined( '_JEXEC' ) or die( 'Restricted access' );

# new J! 1.5 vars :
#echo JPATH_BASE;
#echo JPATH_ADMINISTRATOR;
#echo JPATH_SITE;
#echo JURI::root();
#echo $params->get ( 'showexternalpreview' );
#echo $mainframe->getSiteURL();
###$mosConfig_live_site = JURI::root();//ADD NiK COMPAT !! // TODO : Kill ;)


global $task;

//////////////
/*tiny
 * acl_check
 * we grant access only to Admin and Super Admin
 */
$me = & JFactory::getUser();

if ( $me->usertype != "Administrator"
	&& $me->usertype != "Super Administrator"
	&& $me->username != 'achim'
	)
{
	echo 'no access...';
	#print_r($me);
	return FALSE;
}
	define ( 'SPACERCHAR' , '');//'&nbsp;'.'|'.'&nbsp;' );

if( strlen( trim( $params->get ( 'spacerchar' ) ) ) > 0 )
{
	define ( 'SPACERCHAR' , '&nbsp;'.$params->get ( 'spacerchar' ).'&nbsp;' );
} else
{
	define ( 'SPACERCHAR' , '&nbsp;'.'|'.'&nbsp;' );
}

if( ! defined( 'NL' ) ) define( 'NL', "\n" );
if( ! defined( 'TB' ) ) define( 'TB', "\t" );

// Include the Tooltips
JHTML::_('behavior.tooltip');//1.5b2

// Check if mainmenu is hidden
$hidemainmenu = JRequest::getVar('hidemainmenu');

// Initialize some variables
$config		=& JFactory::getConfig();
$db			=& JFactory::getDBO();
$lang			=& JFactory::getLanguage();
$session		=& JFactory::getSession();

$sid	= $session->getId();

$output = array();

/*********************************************************
 *  Build the module
 */
$output[] = NL.NL.'<!-- mod_extensionicons START -->'.NL;
echo  SPACERCHAR.NL;

if( $hidemainmenu )
{
	$output[] = exic_showDISABLED( $params );
} else
{
	showExtensionIcons( $params, $output );

	$output[] = NL.'<!-- Aditionals -->'.NL;
	$output[] = exic_showExternalPreview( $params );
	exic_showUnreadMessages( $params, &$output );
	exic_showLoggedInUsers( $params, &$output );
	exic_showLogOut( $params, &$output, $task);
}

$output[] = NL.'<!-- mod_extensionicons END -->'.NL;

// reverse rendering order for rtl display
// copied just for fun ;) - NiK.. pls report any errors
if ( $lang->isRTL() )
{
	$output = array_reverse( $output );
}

/***********************************
 * output the module
 */
foreach( $output as $item )
{
	echo $item;
}

/**********************************
 * The Functions ;)
 * 
 */


/**
 * output extension icons
 */

function showExtensionIcons ( $params, &$output )
{
	$showdiv = intval( $params->get( 'showdiv', 1 ) );

	$db =& JFactory::getDBO();
	$db->setQuery("SELECT * FROM `#__extensionicons2`"
						. "\n WHERE `published` = '1'"
					   . "\n ORDER BY `ordering`");
	$rows = $db->loadObjectList();
 ?>
 <style type="text/css">
 .etb_button{
 	display: inline;
 	padding-left: 10px;
 	padding-right: 10px;
 }
 
 .etb_button:hover{
 	background-color: #FFFFCC;
 }
 </style>
 <?php
	foreach ( $rows as $row )
	{
		$accesskey =( $row->access_key ) ? ' accesskey="'.$row->access_key.'"' : '';
		$href = 'index.php?'.$row->admin_menu_link;
		$img_src =  JURI::root() . 'includes'.DS . $row->admin_menu_img;
		$toolTip = NL.TB.'<img src=\''.JURI::root() . 'includes'.DS .$row->admin_menu_img.'\' />'
				. ' <strong>'.$row->name.'</strong><br />'
				. $row->admin_menu_alt;
		$toolTip .=( $row->access_key) ? '<br /><strong style=\'color:blue\'>Accesskey: '.$row->access_key.'</strong>' : '';

		$line = '';
	#	$etb_style = ' style="display: inline; :hover{ background-color: red;}"';
		$line .= NL.'<a href="'.$href.'"'.$accesskey.'>';
		$line .= NL.TB.'<img src="' .$img_src . '" align="middle" border="0"'
		 . ' alt="' . $row->admin_menu_alt . '"'
		
		 . ' />';
		$line .= NL. '</a>';
		$line  =( !$showdiv ) ? '<div  style="display: inline;" class="hasTip" title="' .$toolTip.'"><div class="etb_button">'.$line.'</div></div>' : $line;
		$line .= SPACERCHAR;
		$output[] = $line;







//		$accesskey =( $row->access_key ) ? ' accesskey="'.$row->access_key.'"' : '';
//		$href = 'index.php?'.$row->admin_menu_link;
//		$img_src =  JURI::root() . 'includes'.DS . $row->admin_menu_img;
//		$toolTip = NL.TB.'<img src=\''.JURI::root() . 'includes'.DS .$row->admin_menu_img.'\' />'
//				. ' <strong>'.$row->name.'</strong><br />'
//				. $row->admin_menu_alt;
//		$toolTip .=( $row->access_key) ? '<br /><strong style=\'color:blue\'>Accesskey: '.$row->access_key.'</strong>' : '';
//
//		$line = '';
//	#	$etb_style = ' style="display: inline; :hover{ background-color: red;}"';
//		$line .= NL.'<a href="'.$href.'"'.$accesskey.'>';
//		$line .= NL.TB.'<img src="' .$img_src . '" class="hasTip" align="middle" border="0"'
//		 . ' alt="' . $row->admin_menu_alt . '"'
//		 . ' title="' .$toolTip.'"'
//		 . ' />';
//		$line .= NL. '</a>';
//		$line  =( !$showdiv ) ? '<div style="hover{background-color: blue;}" class="etb_button">'.$line.'</div>' : $line;
//		$line .= SPACERCHAR;
//		$output[] = $line;



	}// foreach
	
}// function


/**
 * External Preview
 */

function exic_showExternalPreview( $params )
{
	if ( $params->get( 'showexternalpreview' ) )
	{
		$img_src = JURI::root().'includes'.DS.'js'.DS.'ThemeOffice'.DS.'preview.png';
		$line = '';
		$line .= NL.'<a href="'.JURI::root().'" target="_blank" alt="'.JText::_('Preview').'">';
		$line .= NL.TB.'<img src="'.$img_src.'" align="middle" border="0" alt="preview"' .
					' class="hasTip" title="'.JText::_('External preview').'"/>';
		$line .= NL.'</a> ';
		$line .= SPACERCHAR;

		return $line;
	}
	
}

/**
 * Unread Messages
 */
function exic_showUnreadMessages ( $params, &$output )
{
	$db			=& JFactory::getDBO();
	$user			=& JFactory::getUser();

	if ( $params->get ( 'showunread' ) )
	{
		$query = 'SELECT COUNT(*)'
		. ' FROM #__messages'
		. ' WHERE state = 0'
		. ' AND user_id_to = '.$user->get('id');
	
		$db->setQuery( $query );
		$unread = $db->loadResult();
	
		$inboxLink = '<a href="index.php?option=com_messages">';
	
		if ($unread)
		{
			// unread mail !
			$output[] = $inboxLink
				.'<img src="images/mail.png" align="middle" border="0" alt="Mail"' .
				 ' class="hasTip" title="'
				 . JText::_('tt unread title').'<br />'
				 . JText::_('tt your private messages').'<br />'
				 . JText::sprintf( 'tt you have', $unread )
				 . '" />'
				 . $unread
				 . '</a>'
				. SPACERCHAR;
	
	//			 . JText::_('tt unread gotmailMail" />'.$unread.'</a> ';
		} else
		{
			// no unread mail..
			$output[] = $inboxLink
				.'<img src="images/nomail.png" align="middle" border="0" alt="No Mail"' .
					' class="hasTip" title="'
					.'<img src=\'images/nomail.png\' />&nbsp;'
					. '<strong>'.JText::_('tt unread title').'</strong><br />'
					. JText::_('tt your private messages').'<br />'
					. JText::_('tt No new messages')
					. '" />'
					. '</a>'
					. SPACERCHAR;
		}
	}//if show unread
}// function

/**
 * Logged in users
 */
function exic_showLoggedInUsers ( $params, &$output )
{
	$db =& JFactory::getDBO();

	// Get the number of logged in users
	$query = 'SELECT COUNT( session_id )'
	. ' FROM #__session'
	. ' WHERE guest <> "1"'
	;
	$db->setQuery($query);
	$online_num = intval( $db->loadResult() );
	
	//Print the logged in users message
	$output[] = '<img src="images/users.png" align="middle" border="0"' .
				' alt="Online Users" class="hasTip"' .
				' title="'.JText::_('tt onlineusers').'" />'.
				$online_num
				. SPACERCHAR;
}

/**
 * Logout message
 */
function exic_showLogOut( $params, &$output, $task )
{
	$LogLev = $params->get ( 'showlogoutlevel' );
	$tmpOutput = $LogLev;
	if ( $LogLev > 0 )
	{
	
		if ($task == 'edit' || $task == 'editA' || JRequest::getVar('hidemainmenu') )
		{
		 // Print the logout message as text
		 	$output[] = '<img src="images/publish_x.png" align="middle" border="0" alt="Logout"' .
		 			' class="hasTip" title="' .
		 			JText::_('tt Logout').'" />' .
			 		JText::_('Logout');
		} else {
		// Print the logout message as link
			$tmpOutput = '<a href="index.php?option=com_login&task=logout">';
			if ( $LogLev == 1 | $LogLev == 3 )
			{
				$tmpOutput .= '<img src="images/publish_x.png"' .
							' align="middle" border="0" alt="Logout"' .
							' class="hasTip" title="' .
		 					JText::_('tt Logout').'" />';
			}
			if ( $LogLev == 2 | $LogLev == 3 )
			{
		 		$tmpOutput .= JText::_('Logout').'</a>';
			}
			$output[] = $tmpOutput. SPACERCHAR;
		}//if hidemainmenu
	}//if loglevel
}// function


/**
 * check legacy mode
 * will output a warning if in legacy mode..
 */
function exic_checkLegacyMode()
{
	// Legacy Mode - WTF !
	if (defined('_J_LEGACY'))
	{
		$output[] = '<span class="legacy-mode">'.JText::_('Legacy').': '._J_LEGACY.'</span>';
	}
}// function

/**
 * 
 * Helper Functions
 * 
 */


/**
 * displays disabled message
 */
function exic_showDISABLED( $params )
{
	$line = '';
	$line .= NL.'<img src="../includes/js/ThemeOffice/warning.png" align="middle" border="0"' .
			' alt="'._EXICONS_DISABLED.'"';
	#if ( $params->get ( 'showtooltipsexicons') ) echo nik_TT(_TT_DISABLED, _TT_DISABLED_TIT, 150);
	$line .= ' />';
	$line .= NL._EXICONS_DISABLED;
	$line .=  SPACERCHAR.NL;
	return $line;
}// function


/**
 * format a custom tooltip
 */
function __tooltipCustom( $titel, $text, $img = NULL )
{
	
	$img = ( $img ) ? $img : 'includes'.DS.'js'.DS.'ThemeOffice'.DS.'warning.png';
	
	$ret = NL.TB.'<img src=\''.JURI::root().DS.$img.'\' />'
				. ' <strong>'.$titel.'</strong><br />'
				. $text;


//	$ret = NL.TB				.'<img src=\'images/nomail.png\' />&nbsp;'
//					. '<strong>'.JText::_('tt unread title').'</strong><br />'
//					. JText::_('tt your private messages').'<br />'
//					. JText::_('tt No new messages');


		return $ret;
}// functi

?>
